package xcu.lxj.nettychat.chat.service.adapter;

import cn.hutool.core.bean.BeanUtil;
import xcu.lxj.nettychat.chat.domain.entity.Message;
import xcu.lxj.nettychat.chat.domain.enums.MessageStatusEnum;
import xcu.lxj.nettychat.chat.domain.enums.MessageTypeEnum;
import xcu.lxj.nettychat.chat.domain.vo.request.ChatMsgReq;
import xcu.lxj.nettychat.chat.domain.vo.request.TextMsgReq;
import xcu.lxj.nettychat.chat.domain.vo.response.ChatMsgResp;
import xcu.lxj.nettychat.chat.service.strategy.MsgHandlerFactory;
import xcu.lxj.nettychat.chat.service.strategy.msg.AbstractMsgHandler;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @Author XinJian Lei
 * @Date 2024/9/29 16:21
 * @Version 1.0
 */

public class MessageAdapter {

    public static final int CAN_CALLBACK_GAP_COUNT = 100;
    public static Message buildMsgSave(ChatMsgReq request, Long uid) {

        return Message.builder()
                .fromUid(uid)
                .roomId(request.getRoomId())
                .type(request.getMsgType())
                .status(MessageStatusEnum.NORMAL.getStatus())
                .build();

    }
    /**
     * 封装 chatMsgResp
     * @param messages 消息列表
     * @param receiveUid  接收人uid 可以空
     * @return
     */
    public static List<ChatMsgResp> buildMsgResp(List<Message> messages, Long receiveUid) {
        return messages.stream().map(a -> {
                    ChatMsgResp resp = new ChatMsgResp();
                    resp.setFromUser(buildFromUser(a.getFromUid()));
                    resp.setMessage(buildMessage(a, receiveUid));
                    return resp;
                })
                .sorted(Comparator.comparing(a -> a.getMessage().getSendTime())) // 帮前端排好序，更方便它展示
                .collect(Collectors.toList());
    }

    /**
     * 封装 ChatMsgResp.Message
     * @param message
     * @param receiveUid
     * @return
     */
    private static ChatMsgResp.Message buildMessage(Message message, Long receiveUid) {
        ChatMsgResp.Message messageVO = new ChatMsgResp.Message();
        BeanUtil.copyProperties(message, messageVO);
        messageVO.setSendTime(message.getCreateTime());
        AbstractMsgHandler<?> msgHandler = MsgHandlerFactory.getStrategyNoNull(message.getType());
        if (Objects.nonNull(msgHandler)) {
            messageVO.setBody(msgHandler.showMsg(message));
        }
        // 消息标记相关的代码已被移除
        return messageVO;
    }

    /**
     * 封装 ChatMsgResp.UserInfo
     * @param fromUid 发送人uid
     * @return ChatMsgResp.UserInfo
     */
    private static ChatMsgResp.UserInfo buildFromUser(Long fromUid) {
        ChatMsgResp.UserInfo userInfo = new ChatMsgResp.UserInfo();
        userInfo.setUid(fromUid);
        return userInfo;
    }

    public static ChatMsgReq buildAgreeMsg(Long roomId) {
        ChatMsgReq chatMessageReq = new ChatMsgReq();
        chatMessageReq.setRoomId(roomId);
        chatMessageReq.setMsgType(MessageTypeEnum.TEXT.getType());
        TextMsgReq textMsgReq = new TextMsgReq();
        textMsgReq.setContent("我们已经成为好友了，开始聊天吧");
        chatMessageReq.setBody(textMsgReq);
        return chatMessageReq;
    }

}


